package com.zhuliwei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuliwei.domain.ResponseResult;
import com.zhuliwei.domain.dto.TagListDto;
import com.zhuliwei.domain.entity.ArticleTag;
import com.zhuliwei.domain.entity.Tag;
import com.zhuliwei.domain.vo.PageVo;
import com.zhuliwei.domain.vo.TagVo;
import com.zhuliwei.enums.AppHttpCodeEnum;
import com.zhuliwei.exception.SystemException;
import com.zhuliwei.mapper.TagMapper;
import com.zhuliwei.service.ArticleTagService;
import com.zhuliwei.service.TagService;
import com.zhuliwei.utils.BeanCopyUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 标签(Tag)表服务实现类
 *
 * @author 朱力维
 * @since 2022-10-25 14:57:58
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Resource
    private ArticleTagService articleTagService;

    @Override
    public ResponseResult<PageVo> pageTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //分页查询
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(tagListDto.getName()), Tag::getName, tagListDto.getName());
        queryWrapper.like(StringUtils.hasText(tagListDto.getRemark()), Tag::getRemark, tagListDto.getRemark());

        Page<Tag> page = new Page<>();
        page.setCurrent(pageNum);
        page.setSize(pageSize);
        page(page, queryWrapper);
        List<Tag> tags = page.getRecords();
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(tags, TagVo.class);
        //封装数据返回
        PageVo pageVo = new PageVo(tagVos, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag(TagListDto tagListDto) {
        if (!StringUtils.hasText(tagListDto.getName())) {
            throw new SystemException(AppHttpCodeEnum.TAG_NAME_NULL);
        }
        if (tagExist(tagListDto.getName())) {
            throw new SystemException(AppHttpCodeEnum.TAG_EXIST);
        }
        Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
        save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId, Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return tagVos;
    }

    @Override
    @Transactional
    public ResponseResult delTag(List<String> ids) {
        // 删除标签表
        removeByIds(ids);
        // 删除标签文章关联表
        for (String id : ids
        ) {
            LambdaQueryWrapper<ArticleTag> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ArticleTag::getTagId, id);
            articleTagService.remove(lqw);
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateTag(TagListDto tagListDto) {
        if (!StringUtils.hasText(tagListDto.getName())) {
            throw new SystemException(AppHttpCodeEnum.TAG_NAME_NULL);
        }
        String name = getById(tagListDto.getId()).getName();
        if (!name.equals(tagListDto.getName())) {
            if (tagExist(tagListDto.getName())) {
                throw new SystemException(AppHttpCodeEnum.TAG_EXIST);
            }
        }
        Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
        updateById(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult listTagNameAndCount() {
        List<Tag> tagList = list();
        if (!CollectionUtils.isEmpty(tagList)) {
            List<HashMap<String, Object>> hashMapList = tagList.stream().map(tag -> {
                LambdaQueryWrapper<ArticleTag> lqw = new LambdaQueryWrapper<>();
                lqw.eq(ArticleTag::getTagId, tag.getId());
                HashMap<String, Object> map = new HashMap();
                map.put("value", articleTagService.count(lqw));
                map.put("name", tag.getName());
                map.put("id",tag.getId());
                return map;
            }).collect(Collectors.toList());
            return ResponseResult.okResult(hashMapList);
        }
        return ResponseResult.okResult();
    }

    public Boolean tagExist(String tagName) {
        LambdaQueryWrapper<Tag> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Tag::getName, tagName);
        return count(lqw) > 0;
    }
}

